/**
 * 页面模板编译  比如： 将 {name} 替换为 viewModel中的name
 *
 * @returns {jQuery}
 */
$.fn.buildPage = function (oriHtml, viewModel) {

    //取出所有中括号中的字符串
    var reg = /\{(.*?)\}/g;
    var matches = oriHtml.match(reg);
    var html = oriHtml;
    $.each(matches, function (index, item) {
        var paraName = item.substring(1, item.length - 1);
        var value = viewModel[paraName];
        if (value == null) value ="";
        html = html.replace(new RegExp(item,"g"),value)

    });

    return html;
}

/**
 * 获取随机字符串
 * @param len
 * @returns {string}
 */
$.fn.random_string = function (len) {
    len = len || 32;
    var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
    var maxPos = chars.length;
    var pwd = '';
    for (i = 0; i < len; i++) {
        pwd += chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return pwd;
}

/**
 * 获取文件后缀名
 * @param filename
 * @returns {string}
 */
$.fn.get_suffix = function (filename) {
    pos = filename.lastIndexOf('.')
    suffix = ''
    if (pos != -1) {
        suffix = filename.substring(pos)
    }
    return suffix;
}

$.fn.getOssTempUrl = function (key,callBack) {
    $.ajax({
        url: '/wechart/operation/ajax-oss/presigned/url',
        dataType: 'JSON',
        method: "POST",
        data: {
            key: key,
        }
    }).done(function (data) {
        if (data.status) {
            if (callBack != null){
                callBack({
                    ossKey:key,
                    ossUrl:data.data
                });
            }
        }
        else {
            $.toast("获取ossTempUrl失败");
        }
    }).fail(function (a, b, c) {
        $.toast("获取ossTempUrl失败");
    });

}

//展开菜单
$.fn.unfoldMenu = function (src) {
    $link = $("#side-menu a[href='" + src + "']" );
    $link.addClass("active");
    $link.parents(".nav.nav-second-level.collapse").addClass("in");
};


function stringFormat() {
    if (arguments.length == 0)
        return null;
    var str = arguments[0];
    for (var i = 1; i < arguments.length; i++) {
        var re = new RegExp('\\{' + (i - 1) + '\\}', 'gm');
        str = str.replace(re, arguments[i]);
    }
    return str;
}















